﻿/***********************************************************************
 * <copyright file="PermissionRepository.cs" company="B/S JSC">
 * -->    Copyright (C) statement. All right reserved
 * </copyright>
 * 
 * Created:   Hoang Bich Son 
 * Email:    sonhb@newman.vn
 * Website:
 * Create Date: Wednesday, March 19, 2014
 * Usage: 
 * 
 * RevisionHistory: 
 * Date         Author               Description 
 * 
 * ************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using DxMvcApplication.Repositories.Core;
using DxMvcApplication.Repositories.Core.Security;
using DxMvcApplication.SecurityServiceReference;
using NmResources;

namespace DxMvcApplication.Repositories.Implementations.Security
{
    /// <summary>
    /// PermissionRepository class
    /// </summary>
    public class PermissionRepository : SecurityRepositoryBase, IPermissionRepository
    {
        public int GetPageSize()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Gets the list.
        /// </summary>
        /// <param name="criterion">The criterion.</param>
        /// <returns></returns>
        public List<PermissionDto> GetList(Criterion criterion = null)
        {
            var request = new PermissionRequest().Prepare();
            request.LoadOptions = new[] { "Permissions" };
            var response = SecurityServiceClient.GetPermissions(request);
            Correlate(request, response);
            return response.Permissions == null ? null : response.Permissions.ToList();
        }
        

        /// <summary>
        /// Gets the paged.
        /// </summary>
        /// <param name="totalPage">The total page.</param>
        /// <param name="currentPage">The current page.</param>
        /// <returns></returns>
        public List<PermissionDto> GetPaged(ref int totalPage, int currentPage = 1)
        {
            var request = new PermissionRequest().Prepare();
            request.LoadOptions = new[] { "Permissions" };
            request.OptionTypes = OptionType.IsPaging;
            request.CurrentPage = currentPage;
            request.TotalPage = totalPage;
            var response = SecurityServiceClient.GetPermissions(request);
            Correlate(request, response);
            totalPage = response.TotalPage;
            return response.Permissions == null ? null : response.Permissions.ToList();
        }

        /// <summary>
        /// Gets the specified identifier.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public PermissionDto Get(int id)
        {
            var request = new PermissionRequest().Prepare();
            request.LoadOptions = new[] { "Permission" };
            request.Criteria = new PermissionCriteria{ Id = id };
            var response = SecurityServiceClient.GetPermissions(request);
            Correlate(request, response);
            return response.Permission;
        }       

        /// <summary>
        /// Gets the paged.
        /// </summary>
        /// <param name="currentPage">The current page.</param>
        /// <returns></returns>
        public List<PermissionDto> GetPaged(int currentPage = 1)
        {
            var request = new PermissionRequest().Prepare();
            request.LoadOptions = new[] { "Permissions" };
            request.OptionTypes = OptionType.IsPaging;
            request.CurrentPage = currentPage;
            var response = SecurityServiceClient.GetPermissions(request);
            Correlate(request, response);
            return response.Permissions == null ? null : response.Permissions.ToList();
        }                 

        /// <summary>
        /// Inserts the specified t.
        /// </summary>
        /// <param name="t">The t.</param>
        /// <returns></returns>
        public string Insert(PermissionDto t)
        {
            var request = new PermissionRequest().Prepare();
            request.Action = "Insert";
            request.Permission = t;
            var response = SecurityServiceClient.SetPermissions(request);
            Correlate(request, response);
            // These messages are for public consumption. Includes validation errors.
            if (response.Acknowledge == AcknowledgeType.Failure)
                throw new ApplicationException(response.Message);
            return response.MessageCode.MapperResource();
        }

        /// <summary>
        /// Updates the specified t.
        /// </summary>
        /// <param name="t">The t.</param>
        /// <returns></returns>
        public string Update(PermissionDto t)
        {
            var request = new PermissionRequest().Prepare();
            request.Action = "Update";
            request.Permission = t;
            var response = SecurityServiceClient.SetPermissions(request);
            Correlate(request, response);
            // These messages are for public consumption. Includes validation errors.
            if (response.Acknowledge == AcknowledgeType.Failure)
                throw new ApplicationException(response.Message);
            return response.MessageCode.MapperResource();
        }

        /// <summary>
        /// Deletes the specified identifier list.
        /// </summary>
        /// <param name="idList">The identifier list.</param>
        /// <returns></returns>
        public string Delete(IList<int> idList)
        {
            var request = new PermissionRequest().Prepare();
            request.Action = "Delete";
            request.Criteria = new PermissionCriteria { IdList = string.Join(",", idList) };
            var response = SecurityServiceClient.SetPermissions(request);
            Correlate(request, response);
            // These messages are for public consumption. Includes validation errors.
            if (response.Acknowledge == AcknowledgeType.Failure)
                throw new ApplicationException(response.Message);
            return response.MessageCode.MapperResource();
        }

        /// <summary>
        /// Checks the exists in role in site.
        /// </summary>
        /// <param name="siteId">The site identifier.</param>
        /// <param name="perCode">The per code.</param>
        /// <param name="userId">The user identifier.</param>
        /// <returns></returns>
        public PermissionDto CheckExistsInRoleInSite(int siteId, string perCode, int userId)
        {
            var request = new PermissionRequest().Prepare();
            request.LoadOptions = new[] { "CheckExistsInRoleInSite" };
            request.Criteria = new PermissionCriteria { SiteId = siteId, Code = perCode, UserId = userId };
            var response = SecurityServiceClient.GetPermissions(request);
            Correlate(request, response);
            return response.Permission;
        }

        /// <summary>
        /// Gets the list by user and site.
        /// </summary>
        /// <param name="userId">The user identifier.</param>
        /// <param name="siteId">The site identifier.</param>
        /// <returns></returns>
        public IList<PermissionDto> GetListByUserAndSite(int userId, int siteId)
        {
            var request = new PermissionRequest().Prepare();
            request.LoadOptions = new[] { "GetByUserAndSite" };
            request.Criteria = new PermissionCriteria { UserId = userId, SiteId = siteId };
            var response = SecurityServiceClient.GetPermissions(request);
            Correlate(request, response);
            return response.Permissions;
        }

        /// <summary>
        /// Grants the permission.
        /// </summary>
        /// <param name="objs">The objs.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public string GrantPermission(List<PermissionInSiteDto> objs)
        {
            throw new NotImplementedException();
        }
    }
}